﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

using Architecture.Implementation.Console.EntityFramework;
using Architecture.Implementation.Console.UnityInterception;
using Architecture.CrossCutting.Tools.Interfaces;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;
using Architecture.CrossCutting.Tools;
using System.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.Diagnostics.Tracing;
using System.Diagnostics;

namespace Architecture.Implementation.Console
{
    class Program
    {
        static void Main(string[] args)
        {

            //            using (var session = new TraceEventSession("ArchitectureResearchSession", @"D:\temp\Logs\ArchitectureResearch.etl"))
            //if file : No session.Source access


            if (!(TraceEventSession.IsElevated() ?? false))
            {
                System.Console.WriteLine("To turn on ETW events you need to be Administrator, please run from an Admin process.");
                Debugger.Break();
                return;
            }

            using (var session = new TraceEventSession("ArchitectureResearchSession"))
            {

                System.Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) { session.Dispose(); };

                session.Source.Dynamic.All += delegate (TraceEvent data)
                {
                    // ETW buffers events and only delivers them after buffering up for some amount of time.  Thus 
                    // there is a small delay of about 2-4 seconds between the timestamp on the event (which is very 
                    // accurate), and the time we actually get the event.  We measure that delay here.     
                    var delay = (DateTime.Now - data.TimeStamp).TotalSeconds;
                    System.Console.WriteLine("GOT Event Delay={0:f1}sec: {1} ", delay, data.ToString());
                };

                session.Source.UnhandledEvents += delegate (TraceEvent data)
                {
                    if ((int)data.ID != 0xFFFE)         // The EventSource manifest events show up as unhanded, filter them out.
                        System.Console.WriteLine("GOT UNHANDLED EVENT: " + data.Dump());
                };

                session.EnableProvider("ArchitectureResearch-Webapi-Tracing");
                session.Source.Process();
                //System.Threading.Thread.Sleep(10000);
            }

            /*
            var listener1 = new ObservableEventListener();
            listener1.LogToConsole();
            listener1.EnableEvents(TracingMethodEventSource.Log, EventLevel.LogAlways);
            */

            /*
            Microsoft.Practices.Unity.UnityContainer container = new Microsoft.Practices.Unity.UnityContainer();
            container.AddNewExtension<Interception>();

            container.RegisterType<ILogger, LocalLogger>();

            container.RegisterType<IMeasurePerformance, MeasurePerformance>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior<LoggingInterceptorBehavior>());



            IMeasurePerformance performanceTester = container.Resolve<IMeasurePerformance>();
            performanceTester.InitEnvironment();

            for(Int32 i=0;i<10;i++)
            { 
                performanceTester.SingleCallQuery();
            }
            */
            System.Console.ReadLine();
            
        }
    }
}
